<template>
  <view>
    <view>
      <t-input
        t-class="cell-title"
        label="旧密码"
        type="password"
        :value="oldPassword"
        placeholder="请输入旧密码"
        @change="oldPasswordInput"
      ></t-input>
      <t-input
        t-class="cell-title"
        label="新密码"
        type="password"
        :value="newPassword"
        placeholder="请输入新密码"
        @change="newPasswordInput"
      ></t-input>
    </view>
    <!-- <view class="tips">需由6-20位数字、字母或符号组成，至少两种</view> -->
    <view class="btn_wrap">
      <t-button
        class="save__btn"
        @tap.native="saveInfo"
        theme="primary"
        size="medium"
        block
        :disabled="!newPassword || !oldPassword"
        >保存</t-button
      >
    </view>
  </view>
</template>

<script>
import { resetUserPassword } from "../../../api/user";
import { Base64 } from "js-base64";
// const passwordReg = /^(?![0-9]+$)(?![a-zA-Z]+$)(?![^0-9a-zA-Z]+$).{6,20}$/;

// 只能是数字、字母、符号
const passwordReg = /^[0-9a-zA-Z~!@#$%^&*()_+|<>?:{}]{1,20}$/;
const app = getApp();
export default {
  components: {},
  data() {
    return {
      oldPassword: "",
      newPassword: "",
    };
  },
  onLoad() {},
  methods: {
    // 校验密码
    checkPassword(password) {
      return passwordReg.test(password);
    },

    oldPasswordInput(e) {
      const oldPassword = e.detail.value;
      this.setData({
        oldPassword,
      });
    },

    newPasswordInput(e) {
      const newPassword = e.detail.value;
      this.setData({
        newPassword,
      });
    },

    async saveInfo() {
      // console.log(this.data.personInfo)
      const { oldPassword, newPassword } = this;
      if (!oldPassword || !newPassword) {
        uni.showToast({
          title: "密码不能为空",
          icon: "error",
        });
        return;
      }
      if (!this.checkPassword(newPassword)) {
        uni.showToast({
          title: "新密码不符合要求",
          icon: "error",
        });
        return;
      }
      if (oldPassword === newPassword) {
        uni.showToast({
          title: "新密码不能与旧密码相同",
          icon: "error",
        });
        return;
      }
      const userInfo = await app.globalData.getUserInfoFn();
      if (!userInfo) {
        return;
      }
      const uid = userInfo.uid;
      if (uid) {
        try {
          const res = await resetUserPassword({
            uid,
            originalPassword: Base64.encode(oldPassword),
            password: Base64.encode(newPassword),
          });

          // console.log('修改密码成功：', res);

          if (res.code !== 0) {
            uni.showToast({
              title: res.msg,
              icon: "error",
            });
            return;
          }
          uni.showToast({
            title: "修改成功",
          });
          setTimeout(() => {
            app.globalData.logoutFn();
          }, 2000);
        } catch (error) {
          console.log("CatchClause", error);
          console.log("CatchClause", error);
          // console.log('修改密码失败：', error);
          uni.showToast({
            title: error.msg || "修改失败",
            icon: "error",
          });
        }
      }
      // wx.navigateBack();
    },
  },
};
</script>
<style lang="less">
.box {
  display: flex;
  justify-content: space-between;
  background-color: #fff;
  width: 500rpx;
}

.btn_wrap {
  padding-bottom: calc(env(safe-area-inset-bottom) + 50rpx);
  bottom: 0;
  position: absolute;
  width: 100%;
}

.save__btn {
  width: 90% !important;
}

.cell-title {
  --td-spacer-2: 92rpx;
}

.tips {
  color: #999;
  font-size: 24rpx;
  margin-top: 20rpx;
  margin-left: 20rpx;
}
</style>
